<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('总体数据分析')" />
    <th:block th:include="include :: bootstrap-select-css" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                        	<li class="select-time">
                                <label>时间范围：</label>
                                <input type="text" class="time-input" id="startTime" placeholder="开始时间" name="startDate"/>
                                <span>-</span>
                                <input type="text" class="time-input" id="endTime" placeholder="结束时间" name="endDate"/>
                            </li>
                            <li>
                                <label class="font-noraml">游戏(可多选)</label>
                                <select class="selectpicker" data-width="300px" name="gameIds" th:with="type=${@dict.loadGames('北凉')}" multiple>
                                    <option th:each="dict : ${type}" th:text="${dict.gameName}" th:value="${dict.id}"></option>
                                </select>
                            </li>
                            <li>
                                渠道商：
                                <select name="channelId" data-width="auto" data-size="10" th:with="type=${@dict.loadChannels('getChannel')}" class="selectpicker" data-live-search="true">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.name}" th:value="${dict.id}"></option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="cwsk:overview:export">
                                    <i class="fa fa-download"></i> 导出
                                </a>
                            </li>
                        </ul>

                    </div>
                </form>
            </div>

            <div class="col-sm-12 link-block">
                    <div class="ibox-content">
                        <div style="height:400px" id="echarts-line-chart"></div>
                    </div>
            </div>

            <div class="col-sm-12 select-table table-bordered">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>

    <th:block th:include="include :: footer" />
    <th:block th:include="include :: echarts-js" />
    <th:block th:include="include :: bootstrap-select-js" />
    <script th:inline="javascript">
        var prefix = ctx + "cwsk/datastat/overview";
        var lineChart;
        var firstLoad = true;

        $(function() {
            var options = {
                url: prefix + "/list",
                exportUrl: prefix + "/export",
                modalName: "总体数据分析",
                sortName: "statDate",
                sortOrder: "desc",
                striped: true,
                pagination: false,
                showToggle: false,
                showRefresh: false,
                showColumns: false,
                showSearch: false,
                onLoadSuccess: onLoadSuccess,
                columns: [
                {
                    title: "序号",
                    formatter: function (value, row, index) {
                        return $.table.serialNumber(index);
                    }
                },
                {
                    field : 'statDate',
                    title : '时间',
                    align: 'center',
                    sortable: true,
                },
                {
                    field : 'regUserCount',
                    title : '新增人数',
                    align: 'center'
                },
                {
                    field : 'activeUserCount',
                    title : '活跃人数',
                    align: 'center'
                },
                {
                    field : 'payCount',
                    title : '充值人数',
                    align: 'center'
                },
                {
                    field : 'incMoney',
                    title : '充值金额',
                    align: 'center'
                },
                {
                    field : 'newIncUserIncMoney',
                    title : '新增充值',
                    align: 'center'
                },
                {
                    field : 'newPayRate',
                    title : '新增付费率',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return keepTwoDecimal(value);
                    }
                },
                {
                    field : 'activePayRate',
                    title : '活跃付费率',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return keepTwoDecimal(value);
                    }
                },
                {
                    field : 'arppu',
                    title : 'ARPPU值',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return value.toFixed(2);
                    }
                },
                {
                    field : 'arpu',
                    title : 'ARPU值',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return value.toFixed(2);
                    }
                },
                {
                    field : 'day1',
                    title : '次留',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return keepTwoDecimal(value);
                    }
                },
                {
                    field : 'day2',
                    title : '3留',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return keepTwoDecimal(value);
                    }
                },
                {
                    field : 'day3',
                    title : '4留',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return keepTwoDecimal(value);
                    }
                },
                {
                    field : 'day4',
                    title : '5留',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return keepTwoDecimal(value);
                    }
                },
                {
                    field : 'day5',
                    title : '6留',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return keepTwoDecimal(value);
                    }
                },
                {
                    field : 'day6',
                    title : '7留',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return keepTwoDecimal(value);
                    }
                },
                {
                    field : 'day7',
                    title : '8留',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return keepTwoDecimal(value);
                    }
                },
                {
                    field : 'day15',
                    title : '15留',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return keepTwoDecimal(value);
                    }
                },
                {
                    field : 'day30',
                    title : '30留',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return keepTwoDecimal(value);
                    }
                }]
            };
            $.table.init(options);
        });


        /* 保留2位小数 百分比*/
        function keepTwoDecimal(value) {
            value=value*100;
            return value.toFixed(2) +"%";
        }

        function onLoadSuccess() {
            drawChart();
        }

        function drawChart() {
            //从表格中获取数据
            var data = $("#" + table.options.id).bootstrapTable('getData');
            var statDate = new Array();
            var regUserCount = new Array();
            var activeUserCount = new Array();
            var payCount = new Array();
            var incMoney = new Array();
            var arppu = new Array();
            var arpu = new Array();

            //删除最后一个 总计数据
            data.pop();

            $.each(data,function(index,item){
                statDate.unshift(item["statDate"]);
                regUserCount.unshift(item["regUserCount"]);
                activeUserCount.unshift(item["activeUserCount"]);
                payCount.unshift(item["payCount"]);
                incMoney.unshift(item["incMoney"]);
                arppu.unshift(parseInt(item["arppu"]));
                arpu.unshift(parseInt(item["arpu"]));
            });

            var lineoption ={};

            //第一次加载，echarts固定数据
            if(firstLoad){
                firstLoad = false;
                lineChart = echarts.init(document.getElementById("echarts-line-chart"));

                lineoption = {
                    title : {
                        text: '总体数据分析'
                    },
                    tooltip : {
                        trigger: 'axis'
                    },
                    legend: {
                        data:['注册用户','活跃用户',"充值人数","充值金额","arppu值","arpu值"]
                    },
                    grid:{
                        left:80,
                        x2:40,
                        y2:24
                    },
                    calculable : true,
                    yAxis : [
                        {
                            type : 'log'

                        }
                    ]
                };
            }

            lineoption.xAxis = [{
                        type : 'category',
                        boundaryGap : false,
                        data : statDate
                    }];
            lineoption.series = [
                    {
                        name:'注册用户',
                        type:'line',
                        data:regUserCount
                    },
                    {
                        name:'活跃用户',
                        type:'line',
                        data:activeUserCount
                    },
                    {
                        name:'充值人数',
                        type:'line',
                        data:payCount
                    },
                    {
                        name:'充值金额',
                        type:'line',
                        data:incMoney
                    },
                    {
                        name:'arppu值',
                        type:'line',
                        data:arppu
                    },
                    {
                        name:'arpu值',
                        type:'line',
                        data:arpu
                    }
                ];

            lineChart.setOption(lineoption);
            $(window).resize(lineChart.resize);
        }


        /*从数据中获取一列数据*/
        function reverseLine(data,field) {
            var array = new Array(data.length);
            $.each(data,function(index,value){
                array.push(value[field]);
            });
            return array;
        }


    </script>
</body>
</html>